package com.xinruke.hostels.common.annotations;

import com.xinruke.hostels.common.util.IDCardNoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @ClassName IDCardNoValidator
 * @CreateDate 2019/7/15
 * @Author FengXinQiang
 * @Version V1.0.0.0
 * @Decription 身份证号码格式验证器
 */
@Slf4j
public class IDCardNoValidator implements ConstraintValidator<IDCardNo, String> {
    @Override
    public void initialize(IDCardNo constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtils.isEmpty(value)) {
            return true;
        }

        String result = IDCardNoUtil.IdentityCardVerification(value);
        if (StringUtils.isEmpty(result)) {
            return true;
        }

        return false;
    }
}
